package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *自定义异常处理类
 */
@Slf4j
@RestControllerAdvice
@Configuration
public class ExceptionCatch {

    /**
     * 用于全局异常处理
     *
     */

    @ExceptionHandler(Exception.class)// 所有子类
    @ResponseBody
    public ResponseResult exception(Exception ex){

        // 记录日志
        log.error("ExceptionCatch ex:{}", ex);

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"您的网络异常,请稍后重试");
    }

    @ExceptionHandler(CustomException.class)
    public ResponseResult customException(CustomException ex){

        System.out.println(ex);
        // 记录日志
        log.error("ExceptionCatch ex:{}",ex);
        AppHttpCodeEnum appHttpCodeEnum = ex.getAppHttpCodeEnum();
        return ResponseResult.errorResult(appHttpCodeEnum);
    }
}
